/**
 * 功能描述 导航栏
 */

define(function (require, exports, module) {
	var Subnav = require("subnav");
	var aw = require("ajaxwrapper")
	var store =require("store");
	var user = require("store").get("user");
	var checkinfile = require("../store/allianceconfirmStore");
	
    var instance = {
        initComponent:function (params,widget){
        	var subnav=new Subnav({
        		parentNode:".J-subnav",
        		model:{
            		title:"入住联盟确认",
            		items:[{
            			id:"servicepoint",
            			type:"buttongroup",
//            			url:"api/servicepoint/operableservicepoints",
            			items:user.privateServicePoints,
            			tip:"服务点",
						keyField :"pkServicePoint",
						valueField :"name",
//            			lazy:true,
            			params:{
							fetchProperties:"pkServicePoint,name"
						},
            			handler:function(key,element){
            				widget.get("grid").refresh({
            					"checkInTypeIn": "Alliance",
                        		"planItemGroup.alliance":false,
	    						"planItemGroup.servicePoint":subnav.getValue("servicepoint"),
	    						"planItemGroup.planItem.startDate": subnav.getValue("checkInDate").start,
	    						"planItemGroup.planItem.startDateEnd": subnav.getValue("checkInDate").end,
	    						"planItemGroup.checkInStatusIn": subnav.getValue("CheckInStatus"),
	    						fetchProperties:checkinfile.getGridFetchProperties()
            				});
            			}
            		},{
            			 id:"CheckInStatus",
			   			 tip:"状态",
			   			 type:'buttongroup',
						 items:[{
								key :"Reserve",
								value:"待确认"
						 },{
								key :"CheckIn",
								value:"已确认"
						 }],
            			handler:function(key,element){
    						widget.get("grid").refresh({
    							"checkInTypeIn": "Alliance",
    	                		"planItemGroup.alliance":false,
    							"planItemGroup.servicePoint":subnav.getValue("servicepoint"),
    							"planItemGroup.planItem.startDate": subnav.getValue("checkInDate").start,
    							"planItemGroup.planItem.startDateEnd": subnav.getValue("checkInDate").end,
    							"planItemGroup.checkInStatusIn": subnav.getValue("CheckInStatus"),
    							fetchProperties:checkinfile.getGridFetchProperties()
						});
    					}
            		},{
            			id:"checkInDate",
						tip:"预约日期",
						type:"daterange",
						ranges:{
					 		"本月": [moment().startOf("month"), moment().endOf("month")],
					 		"上月": [moment().subtract(1,"months").startOf("month"), moment().subtract(1,"months").endOf("month")],
					 		"本周": [moment().startOf("week"), moment().endOf("week")],
					 		"上周": [moment().subtract(1,"week").startOf("week"), moment().subtract(1,"week").endOf("week")]
						},
						handler : function(time){
							 widget.get("grid").refresh({
								 	"checkInTypeIn": "Alliance",
			                		"planItemGroup.alliance":false,
									"planItemGroup.servicePoint":subnav.getValue("servicepoint"),
									"planItemGroup.planItem.startDate": subnav.getValue("checkInDate").start,
									"planItemGroup.planItem.startDateEnd": subnav.getValue("checkInDate").end,
									"planItemGroup.checkInStatusIn": subnav.getValue("CheckInStatus"),
									fetchProperties:checkinfile.getGridFetchProperties()
						});
						}
            		},{
            			id:"save",
    					type:"button",
    					text:"保存",
    					show:false,
    					handler:function(){
    						var data=widget.get("form").getData();
    						var dat=widget.get("optiongrid").getData();
    						var view={};
    						var allianceConfirm={};
    						allianceConfirm.groupReserve={};
    						allianceConfirm.groupReserve.planItemGroup={};
    						allianceConfirm.groupReserve.planItemGroup.pkPlanItemGroup=data.pkPlanItemGroup
    						view.allianceConfirm=allianceConfirm
    						var temp=[];
    						for(var i in dat){
    							var t={};
    							t.signUpRegister={};
    							var d={};
    							d.travelContract=dat[i].travelContract;
    							d.personalInfo=dat[i].personalInfo;
    							d.planItemGroup={};
    							d.planItemGroup.pkPlanItemGroup=data.pkPlanItemGroup
    							d.pkSignUpRegister=dat[i].pkSignUpRegister;
    							d.version=dat[i].version;
    							t.signUpRegister=d;
    							t.signUpRegister.personalInfo.sex=t.signUpRegister.personalInfo.sex.key
    							t.signUpRegister.travelContract.member.personalInfo.sex=t.signUpRegister.personalInfo.sex.key
    							t.signUpRegister.checkInDate = dat[i].checkInDate;
    							t.signUpRegister.checkOutDate = dat[i].checkOutDate;
    							temp.push(t);
    						}
    						view.detail=temp;
    						
    						aw.saveOrUpdate("api/allianceconfirm/saveall",aw.customParam(view),function(data){
    							widget.hide([".J-editgrid",".J-form"]).show([".J-grid"]);
        						widget.get("subnav").hide(["return","save"]).show(["servicepoint","CheckInStatus","checkInDate"]);
    							widget.get("grid").refresh({
    								"checkInTypeIn": "Alliance",
    		                		"planItemGroup.alliance":false,
									"planItemGroup.servicePoint":subnav.getValue("servicepoint"),
									"planItemGroup.planItem.startDate": subnav.getValue("checkInDate").start,
									"planItemGroup.planItem.startDateEnd": subnav.getValue("checkInDate").end,
									"planItemGroup.checkInStatusIn": subnav.getValue("CheckInStatus"),
								fetchProperties:checkinfile.getGridFetchProperties()
    							});
    						});
    					}
            		},{
            			id:"return",
    					type:"button",
    					text:"返回",
    					show:false,
    					handler:function(){
    						widget.hide([".J-editgrid",".J-form"]).show([".J-grid"]);
    						widget.get("subnav").hide(["return","save"]).show(["servicepoint","CheckInStatus","checkInDate"]);
    						/*widget.get("grid").refresh({
								"groupReserve.checkInType": "Alliance",
		                		"planItemGroup.alliance":false,
		                		"planItemGroup.servicePoint":subnav.getValue("servicepoint"),
								"planItemGroup.planItem.startDate": subnav.getValue("checkInDate").start,
								"planItemGroup.planItem.startDateEnd": subnav.getValue("checkInDate").end,
								"planItemGroup.checkInStatusIn": subnav.getValue("CheckInStatus"),
							fetchProperties:checkinfile.getGridFetchProperties()
							});*/
    					}
            		}]
        		}
        	});
        	widget.set("subnav",subnav);

        }
    };
    module.exports = instance;
});
